package com.sevennotes.classtable.ui.pages.classtatistics

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.sevennotes.classtable.config.Config
import com.sevennotes.classtable.model.ClassItem
import com.sevennotes.classtable.model.ClassItemWithName
import com.sevennotes.classtable.model.Time
import com.sevennotes.classtable.utils.TimeUtil

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ClassStatisticsPage(
    classList: List<ClassItemWithName>
) {
    var totalMin = 0
    classList.forEach {
        totalMin += it.classItem.duration
    }
    Column {
        StatisPad(totalNum = classList.size, totalMin = totalMin)
        LazyColumn(modifier = Modifier.padding(horizontal = 10.dp)) {
            val itemCount = classList.size
            var lastWeek: Int? = null
            for (index in 0 until itemCount) {
                //classItem
                val c = classList[index]
                val currentweek = c.classItem.weekDay
                if (currentweek != lastWeek) {
                    stickyHeader {
                        Box(modifier = Modifier
                            .fillMaxWidth()
                            .background(color = Config.dividerColor)) {
                            Text(text = Config.weekdays[currentweek])
                        }
                    }
                }
                item {
                    Divider(Modifier.height(1.dp).background(Config.dividerColor))
                    ClassInfoItem(c)
                }
                lastWeek = currentweek
            }
        }
    }
}

@Composable
fun StatisPad(
    totalNum: Int,
    totalMin: Int
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(60.dp)
            .padding(horizontal = 10.dp),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(text = "总课时数: $totalNum 节", modifier = Modifier.weight(1f))
        Text(text = "总时长: $totalMin 分钟", modifier = Modifier.weight(1f))
    }
}

@Composable
fun ClassInfoItem(item: ClassItemWithName) {
    val timeText = TimeUtil.conversionTimeToString(item.classItem.startTime, item.classItem.duration, 0, false)
    Row(
        modifier = Modifier
            .height(60.dp)
            .background(color = MaterialTheme.colors.background),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Spacer(Modifier.width(10.dp))
        Text(text = item.studentName, modifier = Modifier.weight(1f), textAlign = TextAlign.Start)
        Text(text = timeText, modifier = Modifier.weight(1f), textAlign = TextAlign.End)
        Spacer(Modifier.width(10.dp))
    }
}

@Preview(showBackground = true)
@Composable
fun ClassInfoItemPrev() {
    ClassInfoItem(
        item = ClassItemWithName(
            classItem =  ClassItem(
            duration = 40,
            startTime = Time(8, 40)
            ),
            studentName = "张三",
        )
    )
}